import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:get/get.dart';

import '../../../router/app_pages.dart';
import '../../personnel_module/department_module/department_view.dart';
import '../../personnel_module/personnel_view.dart';
import '../../personnel_module/post_module/post_view.dart';
import '../detail_module/contract_detail_model.dart';

class CreateContractController extends GetxController{



  TextEditingController nameTF = TextEditingController();
  TextEditingController wageTF = TextEditingController();
  TextEditingController notesTF = TextEditingController();

  /// 合同Id,大于0是修改
  int? contractId;

  var startTime = '请选择开始时间';
  var endTime = '请选择结束时间';
  var selectEmployee = '请选择员工';
  var selectEmployeeId;

  var selectDepartment = '请选择部门';
  var selectDepartmentId;

  var selectPost = '请选择岗位';
  var selectId;



  /// 选择开始时间
  void tapClickStartTime()async{
    DatePicker.showDatePicker(Get.context!,
        showTitleActions: true,
        minTime: DateTime(1990, 1, 1),
        maxTime: DateTime(2025, 12, 12), onChanged: (date) {

        }, onConfirm: (date) {
          print('${date.year}-${date.month}-${date.day}');

          startTime = '${date.year}-${date.month}-${date.day}';

          // controller.
          // requestDataWithUpdataInfo(alias_name: controller.editingController.text,
          //     sex: '女',birthday: '${date.year}-${date.month}-${date.day}');
          print('confirm $date');
          update();
        }, currentTime: DateTime.now(), locale: LocaleType.zh);
  }
  /// 选择结束时间
  void tapClickEndTime()async{
    DatePicker.showDatePicker(Get.context!,
        showTitleActions: true,
        minTime: DateTime(1990, 1, 1),
        maxTime: DateTime(2025, 12, 12), onChanged: (date) {

        }, onConfirm: (date) {
          print('${date.year}-${date.month}-${date.day}');

          endTime = '${date.year}-${date.month}-${date.day}';

          // controller.
          // requestDataWithUpdataInfo(alias_name: controller.editingController.text,
          //     sex: '女',birthday: '${date.year}-${date.month}-${date.day}');
          print('confirm $date');
          update();
        }, currentTime: DateTime.now(), locale: LocaleType.zh);
  }
  /// 选择员工
  void tapClickStaff()async{

    var params = {
      'isMultiple':false,
      'personnelIdArr':selectEmployeeId,
      // 'personnelNameArr':userIdName
    };

   var data = await Get.toNamed(AppRoutes.personnel,arguments: params);
   if(data != null){
     selectEmployee = data['personnelNameArr'][0];
     selectEmployeeId = data['personnelIdArr'][0];
     update();
   }

   print('data == = = = =  ${data}');
  }
  /// 选择部门
  void tapClickDepartment()async{
    var data = await Get.to(DepartMentView());
    if(data != null){
      selectDepartment = data['name'];
      selectDepartmentId = data['id'];
    }
    update();
  }

  /// 选择岗位
  void tapClickPost()async{
    var data = await Get.to(PostView());
    if(data != null){
      selectPost = data['name'];
      selectId = data['id'];
    }
    update();
  }
  
  /// 创建请求
  requestDataWithCreate()async{
    var params = {
      'name':'${nameTF.text}',
      'start_time':'${startTime}',
      'end_time':'${endTime}',
      'employee_id':'${selectEmployeeId}',
      'department_id':'${selectDepartmentId}',
      'job_id':'${selectId}',
      'wage':'${wageTF.text}',
      'notes':'${notesTF.text}',
      'resource_calendar_id':'',
      'contract_id':'${Get.arguments['contract_id']}',
    };
    var json = await DioManager().kkRequest(Address.hrContractWrite,bodyParams: params);

    if(json['errmsg']=='ok'){
      BotToast.showText(text: '${json['message']}');
      Get.back(result: 'refresh');
    }
  }

  /// 点击发布
  tapClickDone()async{
    if(nameTF.text==''){
      BotToast.showText(text: '请输入合同名称');
      return;
    }

    requestDataWithCreate();
  }

  /// 获取合同详情；
  ContractDetailModel? model;

  void requestData()async{

    var json = await DioManager().kkRequest('${Address.hrContractRead}/${Get.arguments['id']}',method: 'get');

    ContractDetailModel _model = ContractDetailModel.fromJson(json);

    model = _model;

    nameTF.text = _model.data!.name!;
    wageTF.text = '${_model.data!.wage!}';
    notesTF.text = '${_model.data!.notes!}';

    startTime = _model.data!.dateStart!;
    endTime = _model.data!.dateEnd!;

    update();
  }


  @override
  void onInit() {
    // TODO: implement onInit

    print('contractID === ${Get.arguments}');

    contractId = Get.arguments['contract_id'];

    if(contractId! >0){
      requestData();

    }

    super.onInit();
  }
}